home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / documentation / tutorial / ui / drawing / example / ImageRotator.java < prev    next >
Encoding:
Java Source  |  1997-07-13  |  3.1 KB  |  106 lines

  1. /*
  2.  * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. import java.awt.*;
  18. import java.awt.image.*;
  19. import java.applet.Applet;
  20.  
  21. /* 
  22.  * This applet displays an image. When the user enters an angle,
  23.  * the image is rotated to the specified angle.
  24.  */
  25.  
  26. public class ImageRotator extends Applet {
  27.     TextField degreeField;
  28.     RotatorCanvas rotator;
  29.     double radiansPerDegree = Math.PI / 180;
  30.  
  31.     public void init() {
  32.         // Load the image.
  33.         Image image = getImage(getCodeBase(), "../images/rocketship.gif");
  34.  
  35.         //Set up the UI.
  36.         GridBagLayout gridBag = new GridBagLayout();
  37.         GridBagConstraints c = new GridBagConstraints();
  38.         setLayout(gridBag);
  39.  
  40.         Label l = new Label("Number of degrees to rotate the image:");
  41.         gridBag.setConstraints(l, c);
  42.         add(l);
  43.  
  44.         degreeField = new TextField(5);
  45.         gridBag.setConstraints(degreeField, c);
  46.         add(degreeField);
  47.  
  48.         Button b = new Button("Redraw image");
  49.         c.gridwidth = GridBagConstraints.REMAINDER;
  50.         gridBag.setConstraints(b, c);
  51.         add(b);
  52.  
  53.         rotator = new RotatorCanvas(image);
  54.         c.fill = GridBagConstraints.BOTH;
  55.         c.weightx = 1.0;
  56.         c.weighty = 1.0;
  57.         gridBag.setConstraints(rotator, c);
  58.         add(rotator);
  59.  
  60.         validate();
  61.     }
  62.  
  63.     public boolean action(Event evt, Object arg) {
  64.         int degrees;
  65.  
  66.         try {
  67.             degrees = Integer.parseInt(degreeField.getText());
  68.         } catch (NumberFormatException e) {
  69.             degrees = 0;
  70.         }
  71.  
  72.         //Convert to radians.
  73.         rotator.rotateImage((double)degrees * radiansPerDegree);
  74.  
  75.         return true;
  76.     }
  77. }
  78.  
  79. class RotatorCanvas extends Canvas {
  80.     Image sourceImage;
  81.     Image resultImage;
  82.  
  83.     public RotatorCanvas(Image image) {
  84.         sourceImage = image;
  85.         resultImage = sourceImage;
  86.     }
  87.  
  88.     public void rotateImage(double angle) {
  89.         ImageFilter filter = new RotateFilter(angle);
  90.         ImageProducer producer = new FilteredImageSource(
  91.                                         sourceImage.getSource(),
  92.                                         filter);
  93.         resultImage = createImage(producer);
  94.         repaint();
  95.     }
  96.  
  97.     public void paint(Graphics g) {
  98.         Dimension d = size();
  99.         int x = (d.width - resultImage.getWidth(this)) / 2;
  100.         int y = (d.height - resultImage.getHeight(this)) / 2;
  101.  
  102.         g.drawImage(resultImage, x, y, this); 
  103.     }
  104. }
  105.  
  106.